package formal.hash;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author DengYuan2
 * @create 2021-01-30 20:53
 */
public class E_1 {
    public static void main(String[] args) {
        int[] a = {2,7,11,15};
        int target = 22;
        int[] res = twoSum(a, target);
        System.out.println(Arrays.toString(res));

    }

    /**
     * 我的写法-和大神基本一致
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>(nums.length);
        int[] res = new int[2];
        for (int i = 0; i < nums.length; i++) {
            int value = nums[i];
            if (map.get(target-value)!=null){
                res[0]=map.get(target-value);
                res[1]=i;
                break;
            }else {
                map.put(value,i);
            }
        }
        return res;
    }
}
